package com.znyj.servlet.secondaryController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by gaoliwei02 on 17/6/30.
 */
@Controller
@RequestMapping(value = "/system")
public class SystemController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    protected String index(HttpServletRequest request) throws ParseException, IOException {
        return "/system_page";
    }

    @RequestMapping(value = "/getshell.inner", method = RequestMethod.POST)
    protected @ResponseBody String getShellInner(@RequestBody String receiveData) throws ParseException, IOException {
        JSONObject getJson = JSON.parseObject(receiveData);
        JSONObject json = new JSONObject();

        String process = "/usr/sbin/mysqld";
        String order = "ps aux | grep \"" + process + "\" | grep -v \"grep\"";
        json.put("mysql",shell(order));

        process = "jetty";
        order = "ps aux | grep \"" + process + "\" | grep -v \"grep\"";
        json.put("front",shell(order));

        process = "cloud-device-server";
        order = "ps aux | grep \"" + process + "\" | grep -v \"grep\"";
        json.put("server",shell(order));

        process = "elasticsearch";
        order = "ps aux | grep \"" + process + "\" | grep -v \"grep\"";
        json.put("elasticsearch",shell(order));

        process = "logstash/runner";
        order = "ps aux | grep \"" + process + "\" | grep -v \"grep\"";
        json.put("logstash",shell(order));

//        process = "kibana";
//        order = "ps aux | grep \"" + process + "\" | grep -v \"grep\"";
//        json.put("kibana",shell(order));

        return json.toString();
//        String test = "{\"server\":[{\"RSS\":\"325156\",\"MEM\":\"15.7\",\"CPU\":\"0.1\",\"PID\":\"21429\",\"USER\":\"root\",\"VSZ\":\"1429524\"},{\"RSS\":\"325156\",\"MEM\":\"15.7\",\"CPU\":\"0.1\",\"PID\":\"21429\",\"USER\":\"root\",\"VSZ\":\"1429524\"}],\"mysql\":[{\"RSS\":\"152560\",\"MEM\":\"7.3\",\"CPU\":\"0.0\",\"PID\":\"1073\",\"USER\":\"mysql\",\"VSZ\":\"535592\"}],\"front\":[{\"RSS\":\"274528\",\"MEM\":\"13.3\",\"CPU\":\"2.1\",\"PID\":\"31259\",\"USER\":\"root\",\"VSZ\":\"605480\"}]}";
//        return test;
    }

    public JSONArray shell(String order) {
        Process process = null;
        List<String> processList = new ArrayList<String>();
        String processString = "";
        JSONArray jsonArray = new JSONArray();
        try {
            process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", order});
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                processList.add(line);
                line = line.replaceAll(" +", " ");
                String sub[] = line.split(" ");
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("USER", sub[0]);
                jsonObject.put("PID", sub[1]);
                jsonObject.put("CPU", sub[2]);
                jsonObject.put("MEM", sub[3]);
                jsonObject.put("VSZ", sub[4]);
                jsonObject.put("RSS", sub[5]);
                //jsonObject.put("COMMAND", line);
                jsonArray.add(jsonObject);

            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonArray;
    }
}
